package com.restaurant.application.minifacade.support;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;

public class RequestData implements PickableBean {

    public static final String FN_ACTION = "action";
    public static final String FN_DATA = "data";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_ACTION,
            FN_DATA);

    private String action;
    private String data;

    public RequestData(String action, String data) {
        setAction(action);
        setData(data);
    }

    public RequestData() {
    }

    public RequestData duplicate() {
        RequestData result = new RequestData();
        result.action = this.action;
        result.data = this.data;
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getAction() {
        return action;
    }

    public void setAction(String value) {
        this.action = value;
    }

    public RequestData withAction(String value) {
        setAction(value);
        return this;
    }

    public RequestData withAction(String value, String defaultValue) {
        setAction(value == null ? defaultValue : value);
        return this;
    }

    public String getData() {
        return data;
    }

    public void setData(String value) {
        this.data = value;
    }

    public RequestData withData(String value) {
        setData(value);
        return this;
    }

    public RequestData withData(String value, String defaultValue) {
        setData(value == null ? defaultValue : value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "action":
                return getAction();
            case "data":
                return getData();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof RequestData)) return false;
        return signature().equals(((RequestData) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean RequestData has no signature defined, no hash code or equals possible.");
    }
}
